local meibu = fk.CreateSkill {

  name = "joy__meibu",

  tags = {  },

}



meibu:addEffect(fk.EventPhaseStart, {
  name = "joy__meibu",
  anim_type = "control",
  events = {fk.EventPhaseStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(meibu.name) then
      if event == fk.EventPhaseStart then
        return target.phase == Player.Play and target ~= player and not player:isNude()
      elseif event == fk.AfterCardsMove and player:usedSkillTimes(meibu.name, Player.HistoryTurn) > 0 then
        for _, move in ipairs(data) do
          if move.skillName == "ty__zhixi" then
            for _, info in ipairs(move.moveInfo) do
              if player.room:getCardArea(info.cardId) == Card.DiscardPile then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      local room = player.room
      local card = room:askForDiscard(player, 1, 1, true, meibu.name, true, ".", "#ty__meibu-invoke::"..target.id, true)
      if #card > 0 then
        event:setCostData(self, card[1])
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      room:doIndicate(player.id, {target.id})
      room:throwCard({event:getCostData(self)}, meibu.name, player, player)
      if target.dead then return end
      local turn = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      if turn ~= nil and not target:hasSkill("ty__zhixi", true) then
        room:handleAddLoseSkills(target, "ty__zhixi", nil, true, false)
        turn:addCleaner(function()
          room:handleAddLoseSkills(target, "-ty__zhixi", nil, true, false)
        end)
      end
    else
      local ids = {}
      for _, move in ipairs(data) do
        if move.skillName == "ty__zhixi" then
          for _, info in ipairs(move.moveInfo) do
            if room:getCardArea(info.cardId) == Card.DiscardPile then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
      local dummy = Fk:cloneCard("dilu")
      dummy:addSubcards(ids)
      room:moveCardTo(dummy,Card.PlayerHand,player,fk.ReasonJustMove,meibu.name,nil,true,player)
    end
  end,
})
meibu:addEffect(fk.AfterCardsMove, {
  name = "joy__meibu",
  anim_type = "control",
  events = {fk.EventPhaseStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(meibu.name) then
      if event == fk.EventPhaseStart then
        return target.phase == Player.Play and target ~= player and not player:isNude()
      elseif event == fk.AfterCardsMove and player:usedSkillTimes(meibu.name, Player.HistoryTurn) > 0 then
        for _, move in ipairs(data) do
          if move.skillName == "ty__zhixi" then
            for _, info in ipairs(move.moveInfo) do
              if player.room:getCardArea(info.cardId) == Card.DiscardPile then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      local room = player.room
      local card = room:askForDiscard(player, 1, 1, true, meibu.name, true, ".", "#ty__meibu-invoke::"..target.id, true)
      if #card > 0 then
        event:setCostData(self, card[1])
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      room:doIndicate(player.id, {target.id})
      room:throwCard({event:getCostData(self)}, meibu.name, player, player)
      if target.dead then return end
      local turn = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      if turn ~= nil and not target:hasSkill("ty__zhixi", true) then
        room:handleAddLoseSkills(target, "ty__zhixi", nil, true, false)
        turn:addCleaner(function()
          room:handleAddLoseSkills(target, "-ty__zhixi", nil, true, false)
        end)
      end
    else
      local ids = {}
      for _, move in ipairs(data) do
        if move.skillName == "ty__zhixi" then
          for _, info in ipairs(move.moveInfo) do
            if room:getCardArea(info.cardId) == Card.DiscardPile then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
      local dummy = Fk:cloneCard("dilu")
      dummy:addSubcards(ids)
      room:moveCardTo(dummy,Card.PlayerHand,player,fk.ReasonJustMove,meibu.name,nil,true,player)
    end
  end,
})

return meibu